<!--
 * @Author: Bobby
 * @Date: 2021-12-30 10:01:42
 * @LastEditTime: 2022-02-15 17:59:45
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\ve\elements\other\uiTransfer\uiTransfer.vue
 * CIMC-V2
-->
<template>
  <UiFormItem :class="{ hidden: (info.extension.hidden && info.extension.hidden.value) || conditionProperty.hidden }" :info="info">
    <el-transfer
      :ref="info._id"
      v-model="formDataModel"
      v-ui-ref="vSetRef"
      :button-texts="[info.extension.leftBtn.value, info.extension.rightBtn.value]"
      :data="getData"
      :filterable="info.extension.filterable.value"
      :props="{ key: info.extension.valueField.value, label: info.extension.labelField.value }"
      :target-order="info.extension.targetOrder.value"
      :titles="[info.extension.leftTitle.value, info.extension.rightTitle.value]"
    />
  </UiFormItem>
</template>
<script>
  import { defineComponent } from 'vue'
  import { UiMixin, RefsInjectMixin, ElementsFormMixin, ConditionMixin } from '@ve/mixin'
  import isArray from 'lodash/isArray'
  import merge from 'lodash/merge'
  import isEmpty from 'lodash/isEmpty'
  import trim from 'lodash/trim'
  import request from '@/utils/request'
  import { GFCD } from '@/utils/utils'
  import { processingConstant, replaseRegVariable } from '@ve/utils/utils'

  export default defineComponent({
    name: 'UiTransfer',
    components: {},
    mixins: [UiMixin, RefsInjectMixin, ElementsFormMixin, ConditionMixin],
    data() {
      return {
        formData: [],
      }
    },
    computed: {
      getData() {
        return this.info.data.requestData.data
      },
    },
    // watch: {
    //   getData: {
    //     immediate: true,
    //     handler: function (newval) {
    //       console.log(newval)
    //       this.data = newval
    //     },
    //   },
    // },
    // mounted() {
    //   this.data = this.getData
    // },
    methods: {
      requestData(cb) {
        let requestDataObj = {}
        isArray(this.info.extension.requestData.value) &&
          this.info.extension.requestData.value.forEach((item) => {
            let itemVal = item.value
            if (/#{.*?}/g.test(item.value)) {
              itemVal = replaseRegVariable(itemVal, this.info.data.formData)
            }
            requestDataObj[item.key] = processingConstant(this, itemVal)
          })
        let options = {
          api: this.info.extension.api.value,
          method: this.info.extension.method.value,
          getDataKey: this.info.extension.getDataKey.value,
          data: merge(requestDataObj, this.info.data.formData),
        }
        if (!trim(options.api)) return
        this.request(options, cb)
      },
      // 统一请求
      request(options = {}, cb) {
        let getDataKey = this.info.extension.getDataKey.value
        //发起请求
        return request({
          url: GFCD('dams', 'domain') + options.api,
          method: 'post',
          data: {
            data: JSON.stringify(options.data),
            method: options.method,
          },
        }).then((res) => {
          this.getInfo.data.requestData = isEmpty(getDataKey) ? res.data : res.data[getDataKey]
          if (!isArray(this.getInfo.data.requestData.data)) this.getInfo.data.requestData.data = []
          // 回调
          cb && cb(res)
        })
      },
    },
  })
</script>
